package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.dto.ArticleDto;
import com.heima.article.pojo.ArticleContent;
import com.heima.mapper.ArticleContentMapper;
import com.heima.service.ArticleContentService;
import com.heima.service.ArticleUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ArticleContentServiceImpl extends ServiceImpl<ArticleContentMapper, ArticleContent> implements ArticleContentService {

    @Autowired
    private ArticleUploadService articleUploadService;

    //审核通过自媒体文章，添加或修改文章内容
    public void saveContent(ArticleDto dto) {
        //获取文章内容及文章id
        String content = dto.getContent();
        Long articleId = dto.getId();
        System.out.println(content);
        System.out.println(articleId);


        //创建一个articleContent对象
        ArticleContent articleContent = new ArticleContent();
        //创建构造器判断当前文章内容是否已存在
        LambdaQueryWrapper<ArticleContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleContent::getArticleId, articleId);
        ArticleContent one = getOne(queryWrapper);
        //set数据
        articleContent.setArticleId(articleId);
        articleContent.setContent(content);
        if (one == null) {
            //文章不存在
            save(articleContent);
        } else {
            //文章已存在，做修改操作
            updateById(articleContent);
        }

        //调用方法生成文章详情并上传至minio
        articleUploadService.upLoadArticle(dto);

    }
}
